(*****************************************************************************
 * Delphi Plug-in Framework (DPF)
 * Copyright (C) 2006-2007 Tao Yuan
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the
 * License at http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.                                                               }
 *
 * The Initial Developer of the Original Code is Tao Yuan. Portions created by
 * Tao Yuan are Copyright (C) Tao Yuan (taoy.cn att gmail dott com)
 * All rights reserved.
 *****************************************************************************)
unit RDBaseHandler;

interface

uses
  Classes,
  Rcl;

type
  TBaseHandler = class(TDefaultHandler)
  protected
    log: ILog;
    entityResolver: IEntityResolver;
  public
    constructor Create(anEntityResolver: IEntityResolver);
    procedure error(const e : ISAXParseError); override;
    procedure fatalError(const e : ISAXParseError); override;
    function resolveEntity(const publicId, systemId : SAXString): IInputSource;
        override;
    procedure warning(const e : ISAXParseError); override;
  end;

  TSimpleStack = class(TObject)
  private
    data: IStack;
    function GetSize: Integer;
  public
    constructor Create;
    destructor Destroy; override;
    function pop: TObject;
    procedure push(obj: TObject);
    property Size: Integer read GetSize;
  end;


implementation

uses
  DPFLog;

constructor TSimpleStack.Create;
begin
  data := TStack.Create();
end;

destructor TSimpleStack.Destroy;
begin
  inherited;
end;

function TSimpleStack.GetSize: Integer;
begin
  Result := data.Size;
end;

function TSimpleStack.pop: TObject;
begin
  if data.Empty then
    Result := nil
  else
    Result := data.Pop;
end;

procedure TSimpleStack.push(obj: TObject);
begin
  data.Push(obj);
end;

constructor TBaseHandler.Create(anEntityResolver: IEntityResolver);
begin
  entityResolver := anEntityResolver;
  log := DPFLog.LogFactory.getLogger(Self);
end;

procedure TBaseHandler.error(const e : ISAXParseError);
begin
//  if (entityResolver <> nil) then
      // we are in "validating" mode
//      throw e;

  log.warn('ignoring parse error'{, e}); //$NON-NLS-1$
end;

procedure TBaseHandler.fatalError(const e : ISAXParseError);
begin
  // no op
end;

function TBaseHandler.resolveEntity(const publicId, systemId : SAXString):
    IInputSource;
begin
  if (entityResolver <> nil) then
  begin
      try
        Result := entityResolver.resolveEntity(publicId, systemId);
        Exit;
      except
        on se: ESAXException do
          raise se.Create('');
//        on ioe: EIO do
//          raise ESAXException.Create('I/O error has occurred - ' + ioe, ioe); //$NON-NLS-1$
      end;
  end;
  log.warn('ignoring publicId=' + publicId //$NON-NLS-1$
          + ' and systemId=' + systemId); //$NON-NLS-1$
  Result := nil;
end;

procedure TBaseHandler.warning(const e : ISAXParseError);
begin
  log.warn('non-fatal error while parsing XML document');
end;


end.
